package cn.donghuapian.controller;

import cn.donghuapian.vo.IdVO;
import cn.donghuapian.vo.ResponseBody;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.DateUtil;
import lombok.extern.slf4j.Slf4j;
import org.camunda.bpm.engine.HistoryService;
import org.camunda.bpm.engine.RepositoryService;
import org.camunda.bpm.engine.RuntimeService;
import org.camunda.bpm.engine.TaskService;
import org.camunda.bpm.engine.history.HistoricProcessInstance;
import org.camunda.bpm.engine.repository.DeploymentBuilder;
import org.camunda.bpm.engine.runtime.ProcessInstance;
import org.camunda.bpm.engine.task.Task;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @description:
 * @author: hzw
 * @time: 2024/4/25 15:36
 */
@Slf4j
@RestController
@RequestMapping("/workflow")
public class WorkflowController {

    @Autowired
    private RepositoryService repositoryService;

    @Autowired
    private RuntimeService runtimeService;

    @Autowired
    private TaskService taskService;

    @Autowired
    private HistoryService historyService;

    @PostMapping("/deployByFile")
    public ResponseBody deployByFile(){
        Resource resource = new ClassPathResource("youleyuan.bpmn");
        InputStream inputStream = null;
        try {
            inputStream = resource.getInputStream();
        } catch (IOException e) {
            log.info("千万利器");
        }
        String dateString = DateUtil.format(new Date(), DatePattern.PURE_DATETIME_PATTERN);
        String sourceName = dateString + ".bpmn20.xml";
        DeploymentBuilder builder = repositoryService.createDeployment();
        builder.name("规则审批")
                .source("DEFAULT")
                .tenantId("001")
                .enableDuplicateFiltering(false)
                .addInputStream(sourceName, inputStream)
                .deploy();
        return ResponseBody.success();
    }

    @PostMapping("/start")
    public ResponseBody start() {
        Map<String, Object> map = new HashMap<>();
        map.put("node1", "1");
        map.put("node2", "1");
        map.put("node3", "1");
        map.put("node4", "1");
        runtimeService.startProcessInstanceByKey("youleyuan", map);
        return ResponseBody.success();
    }

    @PostMapping("/complete")
    public ResponseBody complete(@RequestBody IdVO vo) {
        taskService.createTaskQuery().list();
        taskService.complete(vo.getId());
        return ResponseBody.success();
    }

    @PostMapping("/instanceClean")
    public ResponseBody instanceClean() {
        //删除运行中的
        List<ProcessInstance> activeList = runtimeService.createProcessInstanceQuery().active().list();
        if (CollectionUtil.isNotEmpty(activeList)) {
            List<String> activeIds = activeList.stream().map(ProcessInstance::getProcessInstanceId).collect(Collectors.toList());
            runtimeService.deleteProcessInstances(activeIds, "初始化", true, true);
        }
        //删除已完成的
        List<HistoricProcessInstance> completedList = historyService.createHistoricProcessInstanceQuery().list();
        if (CollectionUtil.isNotEmpty(completedList)) {
            List<String> completedIds = completedList.stream().map(HistoricProcessInstance::getId).collect(Collectors.toList());
            historyService.deleteHistoricProcessInstances(completedIds);
        }
        return ResponseBody.success();
    }

}
